﻿using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System.ComponentModel;

namespace Caraya.MFG.BusinessPlugIn
{
    /// <summary>
    /// 直接调拨单
    /// </summary>
    [Description("直接调拨单插件"), HotUpdate]
    public class TransferDirectEdit : AbstractBillPlugIn
    {
        //        public override void DataChanged(DataChangedEventArgs e)
        //        {
        //            base.DataChanged(e);
        //            if (e.Field.Key == "FDestStockId" || e.Field.Key == "FQty")
        //            {
        //                int index = this.View.Model.GetEntryCurrentRowIndex("FBillEntry");
        //                var entry = this.View.BillBusinessInfo.GetEntryEntity("FBillEntry");
        //                // 获取分录当前行数据
        //                DynamicObject currentRow = this.View.Model.GetEntityDataObject(entry, index);
        //                var fStockId = currentRow["DestStockId_Id"];
        //                var fMaterialId = currentRow["MaterialId_Id"];
        //                if (fStockId != null && fMaterialId != null)
        //                {
        //                    var sql1 = string.Format(@"SELECT   SUM(e1.FNoPickedQty) AS NoPickedQty
        //FROM      (SELECT   t1.FENTRYID AS FEntryId, t0.FPRDORGID AS FPrdOrgId, t1_A.FSTATUS AS FStatus
        //                 FROM      T_PRD_MO AS t0 LEFT OUTER JOIN
        //                                 T_PRD_MOENTRY AS t1 ON t0.FID = t1.FID LEFT OUTER JOIN
        //                                 T_PRD_MOENTRY_A AS t1_A ON t1.FENTRYID = t1_A.FENTRYID
        //                 WHERE   (t0.FFORMID = 'PRD_MO')) AS e2 INNER JOIN
        //                    (SELECT   t1.FMOENTRYID AS FMoEntryId1, t1_Q.FNOPICKEDQTY AS FNoPickedQty, 
        //                                     t1.FMATERIALID AS FMaterialID2
        //                     FROM      T_PRD_PPBOM AS t0 LEFT OUTER JOIN
        //                                     T_PRD_PPBOMENTRY AS t1 ON t0.FID = t1.FID LEFT OUTER JOIN
        //                                     T_PRD_PPBOMENTRY_Q AS t1_Q ON t1.FENTRYID = t1_Q.FENTRYID
        //                     WHERE   (t0.FFORMID = 'PRD_PPBOM')) AS e1 ON e2.FEntryId = e1.FMoEntryId1
        //WHERE   (e2.FStatus = N'2' OR e2.FStatus = N'3' OR e2.FStatus = N'4') AND (e1.FMaterialID2 = {0})", fMaterialId);
        //                    var npickedQty = DBServiceHelper.ExecuteScalar(this.Context, sql1, 0d);
        //                    var sql2 = string.Format(@"SELECT   SUM(FBaseQty) AS FQty
        //FROM      (SELECT   FSTOCKORGID AS FStockOrgId, FSTOCKID AS FStockId, FBASEQTY AS FBaseQty, 
        //                                 FSTOCKSTATUSID AS FStockStatusId, FMATERIALID AS FMaterialId
        //                 FROM      T_STK_INVENTORY AS t0
        //                 WHERE   (FOBJECTTYPEID = 'STK_Inventory')) AS e0
        //WHERE   (FStockId = {0}) AND (FMaterialId = {1}) AND (FStockStatusId = 10000) AND (FStockOrgId = 1)", fStockId, fMaterialId);
        //                    var stockQty = DBServiceHelper.ExecuteScalar(this.Context, sql2, 0d);

        //                    var fqty = int.Parse(this.Model.GetValue("FQty", e.Row).ToString());
        //                    if (fqty > npickedQty - stockQty)
        //                    {
        //                        this.View.Model.SetValue("FQty", npickedQty - stockQty, e.Row);
        //                    }
        //                    this.View.InvokeFieldUpdateService("FQty", e.Row);
        //                    this.View.ShowNotificationMessage(string.Format("你可以调拨{0}", npickedQty - stockQty));
        //                }
        //            }
        //        }

        /// <summary>
        /// 值更新时 检查未领料数量减调入仓库库存 控制调拨数量
        /// </summary>
        public override void BeforeUpdateValue(BeforeUpdateValueEventArgs e)
        {
            base.BeforeUpdateValue(e);
            if (e.Key == "FDestStockId" || e.Key == "FQty")
            {
                int index = this.View.Model.GetEntryCurrentRowIndex("FBillEntry");
                var entry = this.View.BillBusinessInfo.GetEntryEntity("FBillEntry");
                DynamicObject currentRow = this.View.Model.GetEntityDataObject(entry, index);
                var fStockId = 0L;
                if (e.Key == "FDestStockId")
                {
                    if (e.Value is DynamicObject v)
                    {
                        fStockId = int.Parse(v[0].ToString());
                    }
                    else if (e.Value is long s)
                    {
                        fStockId = s;
                    }
                }
                else
                {
                    fStockId = int.Parse(currentRow["DestStockId_Id"].ToString());
                }

                var fMaterialId = int.Parse(currentRow["MaterialId_Id"].ToString());
                if (fStockId != 0 && fMaterialId != 0)
                {
                    var sql1 = string.Format(@"SELECT   SUM(e1.FNoPickedQty) AS NoPickedQty
FROM      (SELECT   t1.FENTRYID AS FEntryId, t0.FPRDORGID AS FPrdOrgId, t1_A.FSTATUS AS FStatus
                 FROM      T_PRD_MO AS t0 LEFT OUTER JOIN
                                 T_PRD_MOENTRY AS t1 ON t0.FID = t1.FID LEFT OUTER JOIN
                                 T_PRD_MOENTRY_A AS t1_A ON t1.FENTRYID = t1_A.FENTRYID
                 WHERE   (t0.FFORMID = 'PRD_MO')) AS e2 INNER JOIN
                    (SELECT   t1.FMOENTRYID AS FMoEntryId1, t1_Q.FNOPICKEDQTY AS FNoPickedQty, 
                                     t1.FMATERIALID AS FMaterialID2
                     FROM      T_PRD_PPBOM AS t0 LEFT OUTER JOIN
                                     T_PRD_PPBOMENTRY AS t1 ON t0.FID = t1.FID LEFT OUTER JOIN
                                     T_PRD_PPBOMENTRY_Q AS t1_Q ON t1.FENTRYID = t1_Q.FENTRYID
                     WHERE   (t0.FFORMID = 'PRD_PPBOM')) AS e1 ON e2.FEntryId = e1.FMoEntryId1
WHERE   (e2.FStatus = N'2' OR e2.FStatus = N'3' OR e2.FStatus = N'4') AND (e1.FMaterialID2 = {0})", fMaterialId);
                    var npickedQty = DBServiceHelper.ExecuteScalar(this.Context, sql1, 0d);
                    var sql2 = string.Format(@"SELECT   SUM(FBaseQty) AS FQty
FROM      (SELECT   FSTOCKORGID AS FStockOrgId, FSTOCKID AS FStockId, FBASEQTY AS FBaseQty, 
                                 FSTOCKSTATUSID AS FStockStatusId, FMATERIALID AS FMaterialId
                 FROM      T_STK_INVENTORY AS t0
                 WHERE   (FOBJECTTYPEID = 'STK_Inventory')) AS e0
WHERE   (FStockId = {0}) AND (FMaterialId = {1}) AND (FStockStatusId = 10000) AND (FStockOrgId = 1)", fStockId, fMaterialId);
                    var stockQty = DBServiceHelper.ExecuteScalar(this.Context, sql2, 0d);
                    int fqty = 0;
                    if (e.Key == "FQty" && e.Value != null)
                    {
                        fqty = int.Parse(e.Value.ToString());
                    }
                    else
                    {
                        fqty = int.Parse(this.Model.GetValue("FQty", e.Row).ToString());
                    }

                    if (fqty > npickedQty - stockQty)
                    {
                        e.Cancel = true;
                        this.View.Model.SetValue("FQty", npickedQty - stockQty, e.Row);
                        this.View.InvokeFieldUpdateService("FQty", e.Row);
                        this.View.ShowErrMessage(string.Format("你最多可以调拨{0}", npickedQty - stockQty));
                    }

                }

            }
        }
    }
}
